import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:myflutterapp/demo/layout_demo.dart';
import 'package:myflutterapp/demo/navigator_demo.dart';

main() {
  runApp(MyApp1());
}

class MyApp1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      routes: {"/":(context)=>LoginPage(),
      "layout":(context)=>LayoutDemo()
      },
      initialRoute: "layout",
      onGenerateRoute: (s){
        print(s.name);
        switch(s.name){
          case "menu":
            return MaterialPageRoute(builder: (context){
              return MenuPage();
            },settings: s);
        }
      },
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("你好"),
        elevation: 10.0,
        centerTitle: true,
      ),
      body: InputDemo(),
    );
  }
}

class CountPage extends StatefulWidget {
  @override
  _CountPageState createState() {
    return _CountPageState();
  }
}

class _CountPageState extends State<CountPage> {
  int count = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: [
        Text("$count"),
        RaisedButton(
            onPressed: () {
              setState(() {
                count++;
              });
            },
            child: Container(
              width: double.infinity,
              color: Colors.red,
              child: Text(
                "点击" * 20,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ))
      ],
    );
  }
}

class ButtonDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: [
        MaterialButton(
          onPressed: () {},
          child: Text("点击"),
          color: Colors.red,
        ),
        TextButton(
          onPressed: () {},
          child: Text("点击"),
          style: ButtonStyle(
            //定义文本的样式 这里设置的颜色是不起作用的
            textStyle: MaterialStateProperty.all(
                TextStyle(fontSize: 18, color: Colors.red)),
            //设置按钮上字体与图标的颜色
            //foregroundColor: MaterialStateProperty.all(Colors.deepPurple),
            //更优美的方式来设置
            foregroundColor: MaterialStateProperty.resolveWith(
                  (states) {
                if (states.contains(MaterialState.focused) &&
                    !states.contains(MaterialState.pressed)) {
                  //获取焦点时的颜色
                  return Colors.blue;
                } else if (states.contains(MaterialState.pressed)) {
                  //按下时的颜色
                  return Colors.deepPurple;
                }
                //默认状态使用灰色
                return Colors.grey;
              },
            ),
            //背景颜色
            backgroundColor: MaterialStateProperty.resolveWith((states) {
              //设置按下时的背景颜色
              if (states.contains(MaterialState.pressed)) {
                return Colors.blue[200];
              }
              //默认不使用背景颜色
              return null;
            }),
            //设置水波纹颜色
            overlayColor: MaterialStateProperty.all(Colors.yellow),
            //设置阴影  不适用于这里的TextButton
            elevation: MaterialStateProperty.all(0),
            //设置按钮内边距
            padding: MaterialStateProperty.all(EdgeInsets.all(10)),
            //设置按钮的大小
            minimumSize: MaterialStateProperty.all(Size(200, 100)),

            //设置边框
            side: MaterialStateProperty.all(
                BorderSide(color: Colors.grey, width: 1)),
            //外边框装饰 会覆盖 side 配置的样式
            shape: MaterialStateProperty.all(StadiumBorder()),
          ),
        ),
        IconButton(onPressed: () {}, icon: Icon(Icons.add)),
        OutlinedButton(onPressed: () {}, child: Text("你好")),
        Icon(Icons.home),
        Image.network(
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2018-09-11%2F5b9784aa5ec31.jpg%3Fdown&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638600058&t=fde5ee279d056ee310d0a30e8a197246",
          fit: BoxFit.scaleDown,
        ),
        CheckDemo()
      ],
    );
  }
}

class CheckDemo extends StatefulWidget {
  @override
  _CheckDemoState createState() {
    return _CheckDemoState();
  }
}

class _CheckDemoState extends State<CheckDemo> {
  bool _check = false;
  bool _swtich = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: [
        Checkbox(
          value: _check,
          onChanged: (v) {
            setState(() {
              _check = v!;
            });
          },
        ),
        Switch(
            value: _swtich,
            onChanged: (v) {
              setState(() {
                _swtich = v;
              });
            }),
        ProgressDemo()
      ],
    );
  }
}

class ProgressDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Padding(
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          LinearProgressIndicator(
            value: .5,
            valueColor: AlwaysStoppedAnimation(Colors.red),
          ),
          SizedBox(height: 16),
          Container(
            height: 80,
            width: 80,
            child: CircularProgressIndicator(
              value: .8,
              valueColor: AlwaysStoppedAnimation(Colors.amber),
            ),
          ),
          SizedBox(
            height: 16,
          ),
          CupertinoActivityIndicator(),
        ],
      ),
    );
  }
}

class ClickDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      onTap: () {
        print("tag");
      },
      onDoubleTap: () {
        print("double tag");
      },
      child: Text("data"),
    );
  }
}

class InputDemo extends StatefulWidget {
  @override
  _InputDemoState createState() {
    return _InputDemoState();
  }
}

class _InputDemoState extends State<InputDemo> {
  GlobalKey _key = GlobalKey<FormState>();
  TextEditingController _controller = TextEditingController();
  TextEditingController _userController = TextEditingController();
  FocusNode _u = FocusNode();
  FocusNode _p = FocusNode();
  late FocusScopeNode _focusScopeNode;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    _userController.dispose();
    _u.dispose();
    _p.dispose();
    _focusScopeNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Form(
        key: _key,
        child: Column(
          children: [
            TextFormField(
              focusNode:_u,
              autofocus: true,
              controller: _userController,
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.add),
                labelText: "账号",
                hintText: "请输入账号",
              ),
              obscureText: false,
              validator: (v) {
                if (v == null || v.isEmpty) {
                  return "账号必须输入";
                }
              },
              textInputAction: TextInputAction.next,
              onFieldSubmitted: (v){
                print("object");
              },
            ),
            SizedBox(
              height: 16,
            ),
            TextFormField(
              focusNode:_p,
              controller: _controller,
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.add),
                labelText: "密码",
                hintText: "请输入密码",
              ),
              obscureText: true,
              validator: (v) {
                if (v == null || v.length < 5) {
                  return "密码必须输入且大于5";
                }
              },
              textInputAction: TextInputAction.send,
            ),
            SizedBox(
              height: 16,
            ),
            MaterialButton(
              onPressed: () {
                _focusScopeNode=FocusScope.of(context);
                _focusScopeNode.requestFocus(_u);
                _focusScopeNode.unfocus();
                print((_key.currentState as FormState).validate().toString());
              },
              child: Text("提交"),
              color: Colors.blue,
            )
          ],
        ));
  }
}
